package cn.bytecollege.admin.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.Cuisine;

import java.util.List;

public class CuisineDao extends BaseDao<Cuisine,Integer> {

/**
 * 分页查询菜品信息
 * @author 11819
 */
    public List<Cuisine> findByPage(int pageIndex,int pageSize) {
        String sql="select u.cuisine_id,u.cuisine_name,u.cuisine_price," +
                "u.cuisine_photo,c.category_id,c.category_name from cuisine u inner " +
                "join category c on u.category_id=c.category_id limit ?,?";
        int index=(pageIndex-1)*pageSize;
        return helper.executeQuery(sql,Cuisine.class,index,pageSize);
    }
    /**
     * 统计所有的菜品的数量
     * @author 11819
     */
    public int count(){

        String sql="select count(*) from cuisine";
        return helper.executeScale(sql);
    }

    /**
     * 删除菜品信息根据id删除商品的信息
     * @author 11819
     */
    public int deleteById(Integer cuisineId){
        String sql="delete from cuisine where cuisine_id=?";
        return helper.executeUpdate(sql,cuisineId);
    }
    /**
     *根据id查找到对应的菜品信息
     * @author 11819
     */
    @Override
    public Cuisine findById(Integer cuisineId) {
        String sql="select u.cuisine_id,u.cuisine_name,u.cuisine_price,u.cuisine_photo," +
                "u.category_id,c.category_name from cuisine u left join category c on " +
                "u.category_id=c.category_id where u.cuisine_id=?";
        List<Cuisine> list=helper.executeQuery(sql,Cuisine.class,cuisineId);
        Cuisine cuisine=null;
        if (list.size()>0){
            cuisine= list.get(0);
        }
        return cuisine;
    }
    /**
     * 修改菜品的信息
     * @author 11819
     */
    @Override
    public int update(Cuisine cuisine) {
        String sql="update cuisine set category_id=?,cuisine_name=?,cuisine_price=?,cuisine_photo=? " +
                "where cuisine_id=?";
        Object[] objects={cuisine.getCategoryId(),cuisine.getCuisineName(),cuisine.getCuisinePrice(),cuisine.getCuisinePhoto(),cuisine.getCuisineId()};
        return helper.executeUpdate(sql,objects);
    }
    /**
     * 根据ID查询ID下是否有分类
     * @param categoryId
     * @return
     */
    public List<Cuisine> findByCategoryId(Integer categoryId){
        String sql="select category_id from cuisine where category_id=?";
        return helper.executeQuery(sql,Cuisine.class,categoryId);
    }
    /**
     * 新增菜品
     * @param cuisine
     * @return
     * @Authour: 石晓琳
     */
    @Override
    public int save(Cuisine cuisine) {
        String sql = "INSERT INTO CUISINE(CUISINE_NAME," +
                "CUISINE_PRICE,CATEGORY_ID,CUISINE_PHOTO) " +
                "VALUES (?,?,?,?)";
        Object[] objects = {cuisine.getCuisineName(),cuisine.getCuisinePrice(),
                cuisine.getCategoryId(),cuisine.getCuisinePhoto()};
        return helper.executeUpdate(sql,objects);
    }

}
